package com.ncu.cloundeoffice.controller;


import com.ncu.cloundeoffice.pojo.ChatMsg;
import com.ncu.cloundeoffice.pojo.TAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;


import java.util.Date;

@Controller
public class WsController {




    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;
    @MessageMapping("/ws/chat")
    public void handMsg(Authentication authentication, ChatMsg chatMsg){
        TAdmin tAdmin = (TAdmin)authentication.getPrincipal();
        chatMsg.setFrom(tAdmin.getUsername());
        chatMsg.setFormNickName(tAdmin.getName());
        chatMsg.setDate(new Date());
        simpMessagingTemplate.convertAndSendToUser(chatMsg.getTo(),"/queue/chat",chatMsg);
    }

}
